package com.swak.boot.config.registry;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.swak.Constants;
import com.swak.OS;
import com.swak.registry.RegistryService;
import com.swak.registry.URL;
import com.swak.registry.memory.MemoryRegistry;
import com.swak.utils.Maps;

/**
 * 托底使用的 注册中心
 * 
 * @author 618lf
 */
@Configuration
@ConditionalOnMissingBean(RegistryService.class)
public class MemoryRegistryAutoConfiguration {

	@Autowired
	private ApplicationContext context;

	/**
	 * 创建服务注册和发现
	 * 
	 * @param service
	 * @return
	 */
	@Bean
	@Primary
	public RegistryService memoryRegistry() {
		Map<String, String> parameters = Maps.newHashMap();
		parameters.put(Constants.Application_KEY, context.getEnvironment().getProperty(Constants.Application_Name));
		URL url = new URL(Constants.Registry_Memory_Key, OS.ip(), 0, parameters);
		return new MemoryRegistry(url);
	}
}